package com.gk.init.control
{
	import com.gk.app.CommandList;
	import com.gk.app.SYS;
	import com.gk.init.control.event.InitUserDataEvent;
	import com.gk.init.model.UserInfoModel;
	import com.gk.init.model.vo.UserInfoVo;
	import com.gk.util.net.RemoteExecuter;
	
	import flash.external.ExternalInterface;
	
	import org.robotlegs.mvcs.Command;
	
	/**
	 *用户登录请求命令 
	 * @author Nestor
	 * 
	 */	
	public class UserLoginCommand extends Command
	{
		
		[Inject]
		public var userInfoModel:UserInfoModel;
		
		public function UserLoginCommand()
		{
			super();
		}
		
		override public function execute():void
		{
			
			//renren------http://331king.gotoip4.com/renren/?id=1&origin=50119&xn_sig_in_iframe=1&xn_sig_method=get&xn_sig_time=1335418083846&xn_sig_user=242774893&xn_sig_expires=1335423600&xn_sig_session_key=2.9602520df63bbcfc6f9fe97a95005775.3600.1335423600-242774893&xn_sig_added=1&xn_sig_api_key=5021fd2e112e4bf99a6f07eea79131b0&xn_sig_app_id=189306&xn_sig_domain=renren.com&xn_sig_user_src=rr&xn_sig=33f0b26a732a8794e58ed8e8ac0c5945
			
			if(SYS.plat == SYS.PLAT_RENREN){
				var userIdName:String = "xn_sig_user";
				var sessionIdName:String = "xn_sig_session_key";
			}
			
			if(ExternalInterface.available){
				var param:String = ExternalInterface.call("document.location.search.toString");
				if(Boolean(param)&&param.length>0 && param!=" "){
					param = param.replace("?","");
					var userId:String,sessionId:String;
					var paramArr:Array = param.split("&");
					for each (var p:String in paramArr) 
					{
						if(p.split("=")[0] == userIdName) userId = p.split("=")[1];
						if(p.split("=")[0] == sessionIdName) sessionId = p.split("=")[1];
					}
				}else{
					throw new Error("Don't get param From html");
				}
			}else{
				userId = "242774893";
				sessionId = "2.f39635d1933acadd76b62e8d9d9376e5.3600.1335841200-242774893";
			}
			var obj:Object = {"command":CommandList.INIT_USER_LOGIN,"userId":userId,"sessionId":sessionId};
			new RemoteExecuter(obj,loginOK);
		}
		
		private function loginOK(obj:Object):void
		{
			//{"time":1335416843656,"userId":2}
			userInfoModel.userInfo.user_id = obj.userId;
			
			dispatch(new InitUserDataEvent(InitUserDataEvent.INIT_USER_DATA));
		}
		
	}
}